package com.test.nnooolwdeaer.view_wooden.widget_wooden;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.dialog_wooden.CustomerServiceDialog_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;

public class TitleView_wooden extends LinearLayout {

    private ImageButton backIb_wooden, customerServiceIb_wooden;
    private TextView titleTv_wooden;

    private String title_wooden;
    private boolean showBackBtn_wooden, showCustomerServiceBtn_wooden;
    private boolean isWhiteStyle_wooden;

    public TitleView_wooden(Context context) {
        super(context);
        initView_wooden(context);
    }

    public TitleView_wooden(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs_wooden(context,attrs);
    }

    public TitleView_wooden(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs_wooden(context,attrs);
    }

    private void initAttrs_wooden(Context context, AttributeSet attrs){
        @SuppressLint({"Recycle", "CustomViewStyleable"}) TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.TitleView);
        title_wooden = typedArray.getString(R.styleable.TitleView_title);
        showBackBtn_wooden = typedArray.getBoolean(R.styleable.TitleView_showBackBtn,true);
        showCustomerServiceBtn_wooden = typedArray.getBoolean(R.styleable.TitleView_showCustomerServiceBtn,true);
        isWhiteStyle_wooden = typedArray.getBoolean(R.styleable.TitleView_isWhiteStyle,false);
        initView_wooden(context);
    }

    private void initView_wooden(Context context){
        View view = View.inflate(context,R.layout.view_title_wooden,this);
        backIb_wooden = view.findViewById(R.id.view_title_ib_back);
        titleTv_wooden = view.findViewById(R.id.view_title_tv_title);
        customerServiceIb_wooden = view.findViewById(R.id.view_title_ib_customer_service);

        if (!showBackBtn_wooden) backIb_wooden.setVisibility(GONE);
        if (!showCustomerServiceBtn_wooden) customerServiceIb_wooden.setVisibility(GONE);

        titleTv_wooden.setText(StringUtil_wooden.getSafeString_wooden(title_wooden));

        customerServiceIb_wooden.setOnClickListener(v -> new CustomerServiceDialog_wooden(context).show());

        if (isWhiteStyle_wooden){
            backIb_wooden.setBackgroundResource(R.drawable.icon_back_white);
            titleTv_wooden.setTextColor(Color.WHITE);
            customerServiceIb_wooden.setBackgroundResource(R.drawable.icon_customer_white);
        }

    }

    public void setTitle_wooden(String title_wooden) {
        this.title_wooden = title_wooden;
        titleTv_wooden.setText(StringUtil_wooden.getSafeString_wooden(title_wooden));
    }

    public String getTitle_wooden() {
        return StringUtil_wooden.getSafeString_wooden(title_wooden);
    }

    public ImageButton getBackIb_wooden() {
        return backIb_wooden;
    }
}
